package com.ruoyi.wwc.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MQConfig {
    @Bean //声明正常交换机
    public Exchange orderExchange(){
        return ExchangeBuilder.topicExchange("normal_exchange").durable(true).build();
    }

    @Bean //声明正常队列
    public Queue orderQueue(){
        return QueueBuilder.durable("normal_queue")
                .deadLetterExchange("dead_exchange") //正常队列绑定死信交换机
                .deadLetterRoutingKey("dead.#") //正常队列绑定死信路由键
                .ttl(20*1000) //设置队列的过期时间
                .build();
    }
    //绑定正常交换机和正常队列
    @Bean
    public Binding bindingNormal(){
        return BindingBuilder.bind(orderQueue()).to(orderExchange()).with("normal.#").noargs();
    }

    @Bean //声明死信交换机
    public Exchange deadLetterExchange(){
        return ExchangeBuilder.topicExchange("dead_exchange").durable(true).build();
    }

    @Bean //声明死信队列
    public Queue deadLetterQueue(){
        return QueueBuilder.durable("dead_queue")
                .build();
    }
    //绑定死信交换机和死信队列
    @Bean
    public Binding bindingDeadLetter(){
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange()).with("dead.#").noargs();
    }

}
